/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.goods.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.config.MyProps;
import com.jeesite.modules.goods.entity.ZtCustomer;
import com.jeesite.modules.goods.service.ZtCustomerService;
import com.jeesite.modules.recovery.entity.CustomerType;

/**
 * 测试数据Controller
 * 
 * @author ThinkGem
 * @version 2018-04-22
 */
@Controller
@RequestMapping(value = "${adminPath}/ztCustomer")
public class ZtCustomerController extends BaseController {

	@Autowired
	private ZtCustomerService ztCustomerService;

	@Autowired
	private MyProps myProps;

	/**
	 * 获取数据
	 */
	@ModelAttribute
	public ZtCustomer get(String id, boolean isNewRecord) {

		ZtCustomer ztCustomer = new ZtCustomer();
		ztCustomer.setId(id);
		ztCustomer = ztCustomerService.get(ztCustomer);
		if (ztCustomer == null) {
			ztCustomer = new ZtCustomer();
		}
		return ztCustomer;
	}

	/**
	 * 查询列表
	 */
	@RequiresPermissions("ztCustomer:ztCustomerData:view")
	@RequestMapping(value = { "list", "" })
	public String list(ZtCustomer ztCustomer, Model model) {
		model.addAttribute("ztCustomer", ztCustomer);

		return "modules/ztCustomer/ztCustomerList";
	}

	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("ztCustomer:ztCustomerData:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<ZtCustomer> listData(ZtCustomer ztCustomer,
			HttpServletRequest request, HttpServletResponse response,
			Model model) {

		Page<ZtCustomer> page = ztCustomerService.findPage(new Page<ZtCustomer>(
				request, response), ztCustomer);
	
		
		for(ZtCustomer zc:page.getList()){
			zc.setType(CustomerType.getKey(zc.getType()));
		}

		
		

		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("ztCustomer:ztCustomerData:view")
	@RequestMapping(value = "form")
	public String form(ZtCustomer ztCustomer, Model model,
			HttpServletRequest request, HttpServletResponse response) {
		model.addAttribute("ztCustomer", ztCustomer);

		return "modules/ztCustomer/ztCustomerForm";
	}

	/**
	 * 保存数据
	 */
	@RequiresPermissions("ztCustomer:ztCustomerData:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated ZtCustomer ztCustomer) {

		ztCustomerService.save(ztCustomer);
		return renderResult(Global.TRUE, text("保存成功！"));
	}

	/**
	 * 删除数据
	 */
	@RequiresPermissions("ztCustomer:ztCustomerData:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(ZtCustomer ztCustomer) {
		ztCustomerService.delete(ztCustomer);
		return renderResult(Global.TRUE, text("删除成功！"));
	}
}